﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Drawing;
using System.Data;
using System.Text;
using System.Threading;
using System.Windows.Forms;
using SysMisc;

namespace Kernel
{
    /// <summary>
    /// 实现界面切换的底层容器控件
    /// </summary>
    public partial class UserControlPadContainer : UserControl,IControlPad
    {
        public UserControlPadContainer()
        {
            InitializeComponent();
        }
        public UserControlPadContainer( IApplication app, string WorkFolder )
            :this()/*调用同类中的构造函数*/
        {
            _app = app;
            _work_folder = WorkFolder;
        }

        #region/*实现IControlPad(界面切换)接口*/

        public void Back()
        {
            if( _lst_control.Count > 1)
            {
                /*删除当前视图中的控件*/
                if (null != _uc_current)
                { this.Controls.Remove(_uc_current); }

                /*将控件从队列中删除*/
                int n = _lst_control.Count;
                _lst_control.RemoveAt(n - 1);

                /*加载前一个控件*/
                _uc_current = _lst_control[n - 2];
                this.Controls.Add( _uc_current );
                
                /*更新当前控件的界面UI*/
                IControlEx ui = _uc_current as IControlEx;
                if (null != ui)
                {
                    ui.Update();/*进行界面更新*/
                }
            }
        }
        private delegate void LazyCall(IControlEx ui);
        public void ShowNewPad( UserControl uc )
        {
            if( null != uc)
            {
                _lst_control.Add(uc);

                /*删除当前视图中的控件*/
                if( null != _uc_current)
                {
                    uc.AutoScaleMode = _uc_current.AutoScaleMode;
                    uc.Font = _uc_current.Font;
                    uc.Dock = DockStyle.Fill;   //设置界面未填充满整个区域
                    //uc.BackColor = System.Drawing.Color.Red;  //测试控件的显示范围
                    if (true == this.Controls.Contains(_uc_current))
                        this.Controls.Remove(_uc_current);
                    else
                        SysMisc.LogHelper.Error(typeof(UserControlPadContainer), "Pad中删除当前页面，显示新页面的时候出现错误，Pad中的当前页面不是待删除的页面。");
                }

                /*更新当前视图的寄存变量*/
                this.Controls.Add(uc);
                this._uc_current = uc;

                /*更新当前控件的界面UI*/
                IControlEx ui = _uc_current as IControlEx;
                if( null != ui)
                {
                    try { ui.Init();/*进行初始化*/}catch(Exception ex) { LogHelper.Error(typeof(UserControlPadContainer), ex); }
                    _uc_current.Refresh();
                    //ui.Update();/*进行界面更新(已废弃，因为Update方法包含数据的更新，一般耗时较长，已采用上下文的形式，异步加载)*/

                    _sync_context = SynchronizationContext.Current;
                    _sync_context.Post(LazyLoad, ui);
                }
            }
        }
        private void LazyLoad(object obj)
        {
            IControlEx ui = obj as IControlEx;
            if (null != ui)
            {
                try { 
                    ui.Load();
                    //ui.Update();
                    //_sync_context = SynchronizationContext.Current;
                    _sync_context.Post(LazyUpdate, ui);
                }catch (Exception ex) { LogHelper.Error(typeof(UserControlPadContainer), ex); }
            }
        }
        private void LazyUpdate(object obj)
        {
            IControlEx ui = obj as IControlEx;
            if( null != ui)
            {
                ui.Update();
            }
        }

        public string WorkFolder { get { return _work_folder; } }

        public IApplication App { get { return _app; } }
        /*私有变量*/
        private IApplication _app;
        private string _work_folder;
        private List<UserControl> _lst_control = new List<UserControl>();
        private UserControl _uc_current = null;
        private SynchronizationContext _sync_context = null;
        #endregion
        
    }


    /*创建类型的枚举值*/
    public enum CallTypes
    {
        Create,
        Modify,
        Browser
    }
}
